/*
 * @Descripttion:
 * @Author: tanwei
 * @Date: 2020-07-04 19:28:42
 * @LastEditors: tanwei
 * @LastEditTime: 2020-07-23 11:10:38
 * @FilePath: /egg/admin/app/service/user.js
 */
'use strict';
const Service = require('egg').Service;
class UserAdminService extends Service {
  // 获取单条用户信息
  async show(data) {
    const { app } = this;
    const res = await app.mysql.get('db2').get('ddwx_admin_user', data);
    return res;
  }

  async index(columns, where) {
    const { app } = this;
    const prefix = app.config.mysqlConfig.prefix;
    const res = await app.mysql.select(`${prefix}user`, {
      columns,
      orders: [[ 'id', 'desc' ]],
      where,
    });
    return res;
  }

  async business(data) {
    const { app } = this;
    const res = await app.mysql.get('db2').get('ddwx_business', data);
    return res;
  }

  async showAdminUser(where) {
    const { app } = this;
    const res = await app.mysql.get('db1').get('user', where);
    return res;
  }
  //  添加员工
  async createAdminUser(parmas) {
    const { app: { mysql }, ctx } = this;
    const { helper } = ctx;

    // 创建用户的时候，插入用户角色关联关系
    const res = await mysql.get('db2').beginTransactionScope(async conn => {
      const userData = {
        un: parmas.username,
        pwd: helper.addSaltPassword(parmas.password),
        status: 1,
        aid: 1,
        bid: parmas.bid,
        isadmin: 1,
        addid: 0,
      };
      await conn.insert('ddwx_admin_user', userData);
      return true;
    }, ctx);
    await mysql.get('db1').insert('user', {
      username: parmas.username,
      password: helper.addSaltPassword(parmas.password),
      role_id: parmas.role_id,
      bid: parmas.bid,
      name: parmas.name,
      phone: parmas.phone,
    });
    return res;
  }

  // 查询员工
  // async selectAdminUser(parmas) {
  //   const { app: { mysql } } = this;
  //   const res = await mysql.get('db1').select('user', {
  //     bid: parmas.bid,
  //   });
  //   return res;
  // }
  async selectAdminUser(columns = [], where = {}, page = '', page_size = '', field, order) {
    const { app } = this;
    const options = {
      columns,
      // auth role授权部分排序也要同步调整
      orders: [[ field || 'id', order || 'desc' ]],
      where,
    };
    if (page && page_size) {
      options.limit = page_size * 1;
      options.offset = (page - 1) * page_size;
    }
    const res = await app.mysql.get('db1').select('user', options);
    return res;
  }

  // 修改
  async updateAdminUser(data, where) {
    const { app } = this;
    console.log(data, where);
    const res = await app.mysql.get('db1').update('user', data, {
      where,
    });
    return res.affectedRows === 1;
  }
  // 删除
  async deleteAdminUser(id) {
    const res = await this.app.mysql.get('db1').delete('user', { id });
    return res;
  }

  //  注册商户
  async createBusiness(parmas) {
    const { app: { mysql }, ctx } = this;
    const { helper } = ctx;

    const businessData = {
      aid: 1,
      name: parmas.name,
      linkman: parmas.linkman,
      linktel: parmas.linktel,
      tel: parmas.tel,
    };
    // 创建用户的时候，插入用户角色关联关系
    const res = await mysql.get('db2').beginTransactionScope(async conn => {
      const busubess = await conn.insert('ddwx_business', businessData);
      const userData = {
        un: parmas.username,
        pwd: helper.addSaltPassword(parmas.password),
        status: 1,
        aid: 1,
        bid: busubess.insertId,
        isadmin: 1,
        addid: 0,
      };
      await conn.insert('ddwx_admin_user', userData);
      return true;
    }, ctx);
    await mysql.get('db1').insert('user', {
      username: parmas.username,
      password: helper.addSaltPassword(parmas.password),
      role_id: 0,
      name: parmas.linkman,
      phone: parmas.linktel,
    });
    console.log(res);
    return res;
  }
}
module.exports = UserAdminService;
